package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.KjtAuditStatus;
import com.cloudfun.campusshare.common.constant.SettleType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantChangeSettleTypeDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantChangeSettleTypeReqDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantChangeSettleTypeRespDTO;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.SettleTypeChgApplicationEntity;
import com.cloudfun.campusshare.entity.SubMerchantEntity;
import com.cloudfun.campusshare.repo.KjtSettleTypeChgApplicationRepo;
import com.cloudfun.campusshare.service.KjtService;
import com.cloudfun.campusshare.service.KjtSettleTypeChgApplicationService;
import com.cloudfun.campusshare.service.KjtSubMerchantService;
import com.cloudfun.campusshare.util.AmountUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by CodeGenerator on 2020/08/13
 */
@Slf4j
@Service
public class KjtSettleTypeChgApplicationServiceImpl implements KjtSettleTypeChgApplicationService {

    @Autowired
    private KjtService kjtService;

    @Autowired
    private KjtSubMerchantService subMerchantService;

    @Autowired
    private KjtSettleTypeChgApplicationRepo settleTypeChgApplicationRepo;

    @Override
    @Transactional
    public void changeSettleType(KjtSubMerchantChangeSettleTypeDTO changeSettleTypeDTO) {
        SubMerchantEntity subMerchantEntity = subMerchantService.getEntityByUserId(AuthenticationHolder.getUserId()).orElseThrow(() -> new BusinessException(Code.NODATA));
        // 判断目标结算方式是否和当前结算方式一样
        if (subMerchantEntity.getSettleType() == changeSettleTypeDTO.getSettleType()) {
            throw new BusinessException(Code.ERROR, "目标结算方式是否和当前结算方式相同");
        }
        String subMerchantId = subMerchantEntity.getId();
        // 判断是否已经有进行中的申请
        List<SettleTypeChgApplicationEntity> applicationEntities = settleTypeChgApplicationRepo.findBySubMerchantIdAndStatus(subMerchantId, KjtAuditStatus.P);
        if (CollectionUtil.isNotEmpty(applicationEntities)) {
            throw new BusinessException(Code.ERROR, "当前有正在审核的申请");
        }

        // 存库
        SettleTypeChgApplicationEntity applicationEntity = new SettleTypeChgApplicationEntity();
        applicationEntity.setSubMerchantId(subMerchantId);
        applicationEntity.setUserId(AuthenticationHolder.getUserId());
        applicationEntity.setSettleType(changeSettleTypeDTO.getSettleType());
        if (SettleType.ENTRUST_WITHDRAWAL == changeSettleTypeDTO.getSettleType()) {
            applicationEntity.setEntrustWithdrawalAmount(AmountUtil.strAmount2Int(changeSettleTypeDTO.getEntrustWithdrawalAmount()));
        }
        applicationEntity.setStatus(KjtAuditStatus.P);
        settleTypeChgApplicationRepo.save(applicationEntity);

        // 发起请求
        KjtSubMerchantChangeSettleTypeReqDTO reqDTO = KjtSubMerchantChangeSettleTypeReqDTO.builder()
                .outTradeNo(applicationEntity.getId())
                .partnerUserId(subMerchantId)
                .identityId(subMerchantEntity.getIdentityId())
                .settleType(changeSettleTypeDTO.getSettleType().getValue())
                .build();
        if (SettleType.ENTRUST_WITHDRAWAL == changeSettleTypeDTO.getSettleType()) {
            reqDTO.setEntrustWithdrawalAmount(changeSettleTypeDTO.getEntrustWithdrawalAmount());
        }
        KjtSubMerchantChangeSettleTypeRespDTO respDTO = kjtService.subMerchantChangeSettleType(reqDTO);
        this.handleChangeSettleTypeRespDTO(respDTO);
    }

    @Override
    @Transactional
    public void handleChangeSettleTypeRespDTO(KjtSubMerchantChangeSettleTypeRespDTO respDTO) {
        if (KjtAuditStatus.P == respDTO.getStatus()) {
            return;
        }
        // 修改申请信息
        SettleTypeChgApplicationEntity applicationEntity = settleTypeChgApplicationRepo.findById(respDTO.getOutTradeNo()).orElseThrow(() -> new BusinessException(Code.NODATA));
        applicationEntity.setStatus(respDTO.getStatus());
        applicationEntity.setFailReason(respDTO.getFailReason());

        // 修改结算方式
        SubMerchantEntity subMerchantEntity = subMerchantService.getEntityById(respDTO.getPartnerUserId()).orElseThrow(() -> new BusinessException(Code.NODATA));
        if (KjtAuditStatus.S == respDTO.getStatus()) {
            subMerchantEntity.setSettleType(applicationEntity.getSettleType());
            subMerchantEntity.setEntrustWithdrawalAmount(applicationEntity.getEntrustWithdrawalAmount());
            subMerchantService.save(subMerchantEntity);
        }
    }

}